var width = document.documentElement.clientWidth;
var height =  document.documentElement.clientHeight;

$print =  $('body');

if( width < height ){
    $print.width(height);
    $print.height(width);
    $print.css('marginTop',  (height-width)/2 );
    $print.css('marginLeft',  0-(height-width)/2 );
    $print.css('transform' , 'rotate(90deg)');
    $print.css('transform-origin' , '50% 50%');
    $print.css("border", "2px solid red")
    colScreen();

}else{
    rowScreen();
}

window.addEventListener("orientationchange",(e) => {
    width = document.documentElement.clientWidth;
    height =  document.documentElement.clientHeight;

    if(width < height ){
        $print.width(height-0);
        $print.height(width-0);
        $print.css('marginTop',  0 );
        $print.css('marginLeft',  0 );
        $print.css('transform' , 'rotate(0deg)');
        $print.css('transform-origin' , '0 0');
        rowScreen();
    }else{
        setTimeout(()=>{
            width = document.documentElement.clientWidth;
            height =  document.documentElement.clientHeight;
            $print.width(height-0);
            $print.height(width-0);
            $print.css('marginTop',  (height-width)/2 );
            $print.css('marginLeft',  0-(height-width)/2 );
            $print.css('transform' , 'rotate(90deg)');
            $print.css('transform-origin' , '50% 50%');
            $print.css("border", "2px solid red")
            colScreen();
        },500)
    }
})

var startx, starty;

    //获得角度
    function getAngle(angx, angy) {
        return Math.atan2(angy, angx) * 180 / Math.PI;
    };
 
    //根据起点终点返回方向 1向上 2向下 3向左 4向右 0未滑动
    function getDirection(startx, starty, endx, endy) {
        var angx = endx - startx;
        var angy = endy - starty;
        var result = 0;
        //如果滑动距离太短
        if (Math.abs(angx) < 80 && Math.abs(angy) < 80) {
            return result;
        }
        var angle = getAngle(angx, angy);
        if (angle >= -135 && angle <= -45) {
            result = 1;
        } else if (angle > 45 && angle < 135) {
            result = 2;
        } else if ((angle >= 135 && angle <= 180) || (angle >= -180 && angle < -135)) {
            result = 3;
        } else if (angle >= -45 && angle <= 45) {
            result = 4;
        }
        return result;
    }
    // 横屏函数(1)
    function rowScreen(){
        document.removeEventListener("touchmove",col,false)
        document.addEventListener("touchmove",row,false)
    }
    // 横屏函数(2)
    function row(e){
        var endx, endy;
        endx = e.changedTouches[0].pageX;
        endy = e.changedTouches[0].pageY;
        var direction = getDirection(startx, starty, endx, endy);
        if(moveControl){
            moveControl = false
            setTimeout(() => {
                switch (direction) {
                    case 0:
                        // alert("未滑动！");
                        break;
                    case 1:
                        glUsers.myself.move('up')
                        break;
                    case 2:
                        glUsers.myself.move('down')
                        break;
                    case 3:
                        glUsers.myself.move('left')
                        break;
                    case 4:
                        glUsers.myself.move('right')
                        break;
                    default:
                }
              moveControl = true
            },100)
          }
    }
    // 竖屏函数(1) 与正常不同
    function colScreen(){
        document.removeEventListener("touchmove",row,false)
        document.addEventListener("touchmove",col,false)
    }
    // 竖屏函数(2)
    function col(e){
        var endx, endy;
        endx = e.changedTouches[0].pageX;
        endy = e.changedTouches[0].pageY;
        var direction = getDirection(startx, starty, endx, endy);

        if(moveControl){
            moveControl = false
            setTimeout(() => {
                switch (direction) {
                    case 0:
                        // alert("未滑动！");
                        break;
                    case 1:
                        glUsers.myself.move('left')
                        break;
                    case 2:
                        glUsers.myself.move('right')
                        break;
                    case 3:
                        glUsers.myself.move('down')
                        break;
                    case 4:
                        glUsers.myself.move('up')
                        break;
                    default:
                }
              moveControl = true
            },100)
          }
    }

    //手指接触屏幕
    document.addEventListener("touchstart", function(e) {
        startx = e.touches[0].pageX;
        starty = e.touches[0].pageY;
    }, false);